package com.lanjiu.lib.utils.sp;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.lanjiu.lib.app.App;

import java.util.Set;

/**
 * @description: SharedPreference 工具类
 * @author: Panwei
 * @date: 2019/10/28 19:35
 */
public class SharedPreferenceHelper {

    private static SharedPreferenceHelper mSharedPreferenceHelper;

    public static SharedPreferenceHelper create(String name) {
        if (TextUtils.isEmpty(name)) {
            throw new IllegalArgumentException("The sharedPreferenceHelper name cannot be 'null' or ''");
        }
        if (mSharedPreferenceHelper == null) {
            synchronized (SharedPreferenceHelper.class) {
                if (mSharedPreferenceHelper == null) {
                    // 使用双重同步锁
                    mSharedPreferenceHelper = new SharedPreferenceHelper();
                    initSharedPreference(name);
                } else {
                    initSharedPreference(name);
                }
            }
        } else {
            initSharedPreference(name);
        }

        return mSharedPreferenceHelper;
    }

    private static void initSharedPreference(String name) {
        if (mSharedPreferenceHelper.getSharedPreferences() == null) {
            mSharedPreferenceHelper.init(App.getContext(), name);
        } else {
            if (TextUtils.isEmpty(mSharedPreferenceHelper.getSharedPreferencesName())
                    || !name.equals(mSharedPreferenceHelper.getSharedPreferencesName())) {
                mSharedPreferenceHelper.init(App.getContext(), name);
            }
        }
    }

    private SharedPreferences mSharedPreferences;
    private String mSharedPreferencesName;
    private Editor mSharedPreferencesEditor;

    private void init(Context context, String name) {
        mSharedPreferencesName = name;
        mSharedPreferences = context.getSharedPreferences(mSharedPreferencesName, Context.MODE_PRIVATE);
    }

    private String getSharedPreferencesName() {
        return mSharedPreferencesName;
    }

    private SharedPreferences getSharedPreferences() {
        return mSharedPreferences;
    }

    /**
     * 判断key是否存在
     *
     * @param key
     * @return
     */
    public boolean contains(String key) {
        return mSharedPreferences.contains(key);
    }

    /**
     * 获取对象
     *
     * @param key
     * @return
     */
    public <E> E getObject(String key, Class<E> clazz) {
        String json = mSharedPreferences.getString(key, null);
        if (json == null)
            return null;
        try {
            return new Gson().fromJson(json, clazz);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取String
     *
     * @param key
     * @return
     */
    public String getString(String key) {
        return mSharedPreferences.getString(key, null);
    }

    public String getString(String key, String def) {
        return mSharedPreferences.getString(key, def);
    }

    /**
     * 获取Set<String>
     *
     * @param key
     * @return
     */
    public Set<String> getStringSet(String key) {
        return mSharedPreferences.getStringSet(key, null);
    }

    public Set<String> getStringSet(String key, Set<String> def) {
        return mSharedPreferences.getStringSet(key, def);
    }

    /**
     * 获取Int
     *
     * @param key
     * @return
     */
    public int getInt(String key) {
        return mSharedPreferences.getInt(key, 0);
    }

    public int getInt(String key, int def) {
        return mSharedPreferences.getInt(key, def);
    }

    /**
     * 获取Long
     *
     * @param key
     * @return
     */
    public long getLong(String key) {
        return mSharedPreferences.getLong(key, 0);
    }

    public long getLong(String key, long def) {
        return mSharedPreferences.getLong(key, def);
    }

    /**
     * 获取Float
     *
     * @param key
     * @return
     */
    public float getFloat(String key) {
        return mSharedPreferences.getFloat(key, 0f);
    }

    public float getFloat(String key, float def) {
        return mSharedPreferences.getFloat(key, def);
    }

    public boolean getBoolean(String key) {
        return mSharedPreferences.getBoolean(key, false);
    }

    public boolean getBoolean(String key, boolean def) {
        if (mSharedPreferences == null)
            return def;
        return mSharedPreferences.getBoolean(key, def);
    }

    public Editor editor() {
        if (mSharedPreferencesEditor == null)
            mSharedPreferencesEditor = new Editor(spEditor());
        return mSharedPreferencesEditor;
    }

    private SharedPreferences.Editor spEditor() {
        if (mSharedPreferences == null)
            mSharedPreferences = App.getContext().getSharedPreferences(mSharedPreferencesName, Context.MODE_PRIVATE);
        return mSharedPreferences.edit();
    }

    public void setObject(String key, Object object) {
        String json = new Gson().toJson(object, object.getClass());
        spEditor().putString(key, json).commit();
    }

    public void setString(String key, String value) {
        spEditor().putString(key, value).commit();
    }

    public void setStringSet(String key, Set<String> values) {
        spEditor().putStringSet(key, values).commit();
    }

    public void setInt(String key, int value) {
        spEditor().putInt(key, value).commit();
    }

    public void setLong(String key, long value) {
        spEditor().putLong(key, value).commit();
    }

    public void setFloat(String key, float value) {
        spEditor().putFloat(key, value).commit();
    }

    public void setBoolean(String key, boolean value) {
        spEditor().putBoolean(key, value).commit();
    }

    public void delete(String key) {
        spEditor().remove(key).commit();
    }

    public class Editor {

        private SharedPreferences.Editor mEdit;

        public Editor(SharedPreferences.Editor edit) {
            mEdit = edit;
        }

        public Editor putObject(String key, Object object) {
            String json = new Gson().toJson(object, object.getClass());
            mEdit.putString(key, json);
            return this;
        }

        public Editor putString(String key, String value) {
            mEdit.putString(key, value);
            return this;
        }

        public Editor putStringSet(String key, Set<String> values) {
            mEdit.putStringSet(key, values);
            return this;
        }

        public Editor putInt(String key, int value) {
            mEdit.putInt(key, value);
            return this;
        }

        public Editor putLong(String key, long value) {
            mEdit.putLong(key, value);
            return this;
        }

        public Editor putFloat(String key, float value) {
            mEdit.putFloat(key, value);
            return this;
        }

        public Editor putBoolean(String key, boolean value) {
            mEdit.putBoolean(key, value);
            return this;
        }

        public Editor remove(String key) {
            mEdit.remove(key);
            return this;
        }

        public void commit() {
            mEdit.commit();
        }
    }
}
